package com.mazaiting.redeye.module.me

import cn.jpush.im.android.api.JMessageClient
import cn.jpush.im.android.api.model.UserInfo
import com.mazaiting.easy.base.bean.Response
import com.mazaiting.easy.base.mvp.IBasePresenter
import com.mazaiting.easy.base.mvp.IBaseView
import com.mazaiting.easy.base.presenter.BaseNetPresenter
import com.mazaiting.easy.utils.rx.RxScheduler
import com.mazaiting.log.L
import com.mazaiting.redeye.api.UserApi
import com.mazaiting.redeye.base.ApplicationComponentImpl
import com.mazaiting.redeye.base.BaseCommonPresenter
import com.mazaiting.redeye.base.Config
import com.mazaiting.redeye.base.CustomConsumer
import com.mazaiting.redeye.bean.Request
import com.mazaiting.redeye.bean.UserBean
import com.trello.rxlifecycle3.kotlin.bindToLifecycle
import dagger.Component
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Consumer
import javax.inject.Inject

/***
 *
 *
 *                                                    __----~~~~~~~~~~~------___
 *                                   .  .   ~~//====......          __--~ ~~
 *                   -.            \_|//     |||\\  ~~~~~~::::... /~
 *                ___-==_       _-~o~  \/    |||  \\            _/~~-
 *        __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *    _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *  .~       .~       |   \\ -_    /  /-   /   ||      \   /
 * /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 * |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *          '         ~-|      /|    |-~\~~       __--~~
 *                      |-~~-_/ |    |   ~\_   _-~            /\
 *                           /  \     \__   \/~                \__
 *                       _--~ _/ | .-~~____--~-/                  ~~==.
 *                      ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                 -_     ~\      ~~---l__i__i__i--~~_/
 *                                 _-~-__   ~)  \--______________--~~
 *                               //.-~~~-~_--~- |-------~~~~~~~~
 *                                      //.-~~~--\
 *                               神兽保佑
 *                              代码无BUG!
 * @author mazaiting
 * @date 2019-07-01
 * @description 我的相关
 */
class MeContract {
    interface View : IBaseView {
        /**
         * 加载用户信息成功
         */
        fun loadInformationSuccess()

        /**
         * 加载用户信息失败
         */
        fun loadInformationError(msg: String)
    }
    interface Presenter : IBasePresenter<View> {
        /**
         * 加载用户信息
         */
        fun loadInformation()

        /**
         * 加载头像信息
         */
        fun loadHeaderInfo(): UserInfo
    }
}

/**
 * 我的主持人
 */
class MePresenter
@Inject constructor(
    private val userApi: UserApi
) : BaseCommonPresenter<MeContract.View>(), MeContract.Presenter {

    override fun loadHeaderInfo(): UserInfo = JMessageClient.getMyInfo()

    /**
     * 用户详细信息 Disposable
     */
    private var mInformationDispose: Disposable? = null

    override fun loadInformation() = if (isNetConnected) {
        view.onShowLoading()

        mInformationDispose = userApi.information(Request(token, UserBean(username = username)))
            .compose(RxScheduler.applySchedulers())
            .bindToLifecycle(view.lifecycleProvider)
            .subscribe(object : CustomConsumer<Response<UserBean>> {
                override fun onSuccess(result: Response<UserBean>?) {
                    if (null != result) {
                        if (result.retCode == 1) {
                            // 请求成功
                            view.onShowSuccess()
                            // 登录成功
                            view.loadInformationSuccess()
                        } else {
                            view.onShowFailed(result.msg)
                            view.loadInformationError(result.msg)
                        }
                    } else {
                        view.onShowFailed(result?.msg)
                        view.loadInformationError("请求失败")
                    }
                }
            }, Consumer<Throwable> { e ->
                L.d(e.message)
                e.message?.let { view.loadInformationError(it) }
                view.onShowFailed(e.message)
            })
    } else {
        view.onShowNoNet()
    }

}

/**
 * 我的组件
 */
@Component(dependencies = [ApplicationComponentImpl::class])
interface MeComponent {
    /**
     * 注入我的 Fragment
     */
    fun inject(fragment: MeFragment)
}